//
//  addView.m
//  Estate
//
//  Created by wangdongdong on 2017/9/28.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "SafeViewController.h"

#import "addView.h"

@interface addView()<UITableViewDelegate, UITableViewDataSource>

// 添加视图
@property (strong ,nonatomic) UITableView *tableView;


@end



@implementation addView

// 数据源
- (NSArray *)addArr {
    if (!_addArr) {
        _addArr = [[NSArray alloc] init];
    }
    return _addArr;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(50, 64, DEF_SCREEN_WIDTH - 50, DEF_SCREEN_HEIGHT-64)];
        _tableView.backgroundColor = UIColorFromRGB(0xf2f2f2);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = UIColorFromRGB(0xeeeeee);
        _tableView.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    if (self) {
        // 标题
        UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, DEF_SCREEN_WIDTH-50, 64)];
        [self addSubview:titleView];
        titleView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [titleView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.offset(-15);
            make.centerX.equalTo(titleView.mas_centerX);
        }];
        titleLabel.font = [UIFont systemFontOfSize:18];
        titleLabel.textColor = mainTitleColor;
        titleLabel.text = @"工作";
        
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma MARK - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.addArr.count;
}

// 控制表头分组表头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.addArr.count > 1) {
        return 44;
        
    } else {
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *arr;
    if (self.addArr.count > 0) {
        arr = self.addArr[section];
    }
    return arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = mainTitleColor;
    NSArray *arr;
    if (self.addArr.count > 0) {
        arr = self.addArr[indexPath.section];
    }
    cell.textLabel.text = arr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [UIView animateWithDuration:0.1 animations:^{
        self.frame = CGRectMake(DEF_SCREEN_WIDTH, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
        
    } completion:^(BOOL finished) {
        // 初始化控制器
        NSString *VCStr;
        UIViewController *addVC;
        if ([self.typeStr isEqualToString:@"安全"]) {
            VCStr = self.VCArr[0][0];
            SafeViewController *safeVC = [[NSClassFromString(VCStr) alloc] init];
            safeVC.typeStr = [NSString stringWithFormat:@"%d", indexPath.row+1];
            addVC = safeVC;
        }
        [self.navVC pushViewController:addVC animated:YES];
    }];
}


@end

